// 操作数库的对象  db
// 数据库：blog_database
// 数据表：pre_message

// SELECT  字段
// FROM  数据表
// WHERE  条件
// INSERT  INTO 插入
// INTO   数据表
// UPDATE   数据表  更新
// DELETE   删除
// DELETE FROM 数据表

const db = require("../common/db/index");

module.exports = {
    // 发表留言
    publish(msg_text, createAt, imgUrl, user_id, username) {
        return new Promise(async (resolve, reject) => {
            const sql = "insert into pre_message (msg_text,createAt,imgUrl,user_id,username) values (?,?,?,?,?)";
            await db.query(sql, [msg_text, createAt, imgUrl, user_id, username]).then(
                (result) => {
                    resolve({
                        code: 200,
                        msg: "发表留言成功",
                        result
                    });
                },
                (err) => {
                    reject({
                        code: -1,
                        msg: "发表留言失败",
                        err,
                    })
                }
            )
        })
    },

    // 修改留言
    modify(msg_text, createAt, id) {
        return new Promise(async (resolve, reject) => {
            const sql = "update pre_message  set msg_text=? , createAt=? where id=?";
            await db.query(sql, [msg_text, createAt, id])
                .then(
                    result => {
                        resolve({
                            code: 200,
                            msg: "修改留言成功",
                            result
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "修改留言失败",
                            err
                        })
                    }
                )
        })
    },
    // 删除留言
    remove(id) {
        return new Promise(async (resolve, reject) => {
            let sql = "DELETE FROM pre_message WHERE id = ?";
            await db.query(sql, [id])
                .then(
                    result => {
                        resolve({
                            code: 200,
                            msg: "删除留言成功",
                            result
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "删除留言失败",
                            err
                        })
                    }
                )
        })
    },

    // 获取留言分页
    items(page_num, page_size) {
        return new Promise(async (resolve, reject) => {
            const rows = await db.query(`SELECT count(*) as total FROM pre_message`);
            let page_num2 = ((parseInt(page_num) - 1) * parseInt(page_size))
            let page_size2 = (parseInt(page_size))
            let sql = `select * from pre_message order by createAt DESC limit ${page_num2} , ${page_size2}`
            await db.query(sql)
                .then(
                    result => {
                        resolve({
                            code: 200,
                            total: rows[0].total,
                            msg: "获取留言分页成功",
                            result
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "获取留言分页失败",
                            err
                        })
                    }
                )
        })
    },

    // 搜索留言列表
    search(keyword) {
        return new Promise(async (resolve, reject) => {
            let sql = `select * from pre_message where msg_text like ? or username like ?  or user_id like ? order by createAt DESC`
            await db.query(sql, [`%${keyword}%`, `%${keyword}%`, `%${keyword}%`])
                .then(
                    result => {
                        if (result.length > 0) {
                            resolve({
                                msg: "搜索留言成功",
                                code: 200,
                                result

                            })
                        } else {
                            resolve({
                                msg: "没有搜索到留言",
                                code: -1,
                                result
                            })

                        }
                    },
                    err => {
                        reject({
                            msg: "搜索留言失败",
                            code: -1,
                            err: err
                        })
                    }
                )
        })
    },


    // 获取留言列表
    list() {
        return new Promise((resolve, reject) => {
            // 编写sql语句 （查询语句）
            const sql = 'SELECT id, msg_text,imgUrl FROM pre_message order by createAt DESC';
            // 通过query方法执行sql语句
            db.query(sql)
                .then(
                    result => {
                        resolve({
                            code: 200,
                            msg: "获取留言列表成功",
                            result
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "获取留言列表失败",
                            err
                        })
                    }
                )

        })
    },
};
